package com.lms.netty.tcp.protocol;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ReplayingDecoder;

import java.util.List;

/**
 * @Author: 李孟帅
 * @CreateTime: 2020-06-18 18:00
 * @Description:
 */
public class MyMessageDecoder extends ReplayingDecoder<Void> {
    @Override
    protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
        System.out.println("解码...");
        //根据指定长度读取
        int len = in.readInt();
        byte[] content = new byte[len];
        in.readBytes(content);
        //封装成messageprotocol对象，放入out中，传递给下一个handler业务处理器
        MessageProtocol msg = new MessageProtocol();
        msg.setLen(len);
        msg.setContent(content);
        out.add(msg);
    }
}
